package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.SendHistory;
import com.ruoyi.system.domain.TGroup;
import com.ruoyi.system.dto.GroupSendMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TMessages;
import com.ruoyi.system.service.ITMessagesService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 消息Controller
 * 
 * @author ruoyi
 * @date 2025-08-18
 */
@RestController
@RequestMapping("/system/messages")
@Api(tags = "消息")
public class TMessagesController extends BaseController
{
    @Autowired
    private ITMessagesService tMessagesService;

    /**
     * 查询消息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:messages:list')")
    @GetMapping("/list")
    @ApiOperation("消息列表")
    @Anonymous
    public TableDataInfo list(TMessages tMessages) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int pageNum = pageDomain.getPageNum();
        int pageSize = pageDomain.getPageSize();
        
        TableDataInfo dataTable = tMessagesService.selectTMessagesList(tMessages, pageNum, pageSize);
        return dataTable;
    }

    /**
     * 查询用户消息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:messages:list')")
    @GetMapping("/user/list")
    @ApiOperation("用户消息列表")
    @Anonymous
    public TableDataInfo userList(TMessages tMessages)
    {
        // 使用高效的分表分页查询
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int pageNum = pageDomain.getPageNum();
        int pageSize = pageDomain.getPageSize();

        // 调用Service层的高效分页方法
        TableDataInfo dataTable = tMessagesService.selectTMessagesListWithPaging(tMessages, pageNum, pageSize);

        return dataTable;
    }

    /**
     * 导出消息列表
     */
    @Log(title = "消息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TMessages tMessages)
    {
        // 导出功能必须指定用户ID，避免查询所有128张表导致性能问题
        if (tMessages.getQueryUserId() == null || tMessages.getQueryUserId().trim().isEmpty()) {
            throw new RuntimeException("导出功能必须指定用户ID参数，避免数据量过大");
        }
        List<TMessages> list = tMessagesService.exportTMessagesList(tMessages);
        ExcelUtil<TMessages> util = new ExcelUtil<TMessages>(TMessages.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:messages:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tMessagesService.selectTMessagesById(id));
    }

    /**
     * 新增消息
     */
    @PreAuthorize("@ss.hasPermi('system:messages:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMessages tMessages)
    {
        return toAjax(tMessagesService.insertTMessages(tMessages));
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('system:messages:edit')")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMessages tMessages)
    {
        return toAjax(tMessagesService.updateTMessages(tMessages));
    }

    /**
     * 删除消息
     */
    @Log(title = "消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMessagesService.deleteTMessagesByIds(ids));
    }



    /**
     * 广播消息
     */
    @Log(title = "广播消息", businessType = BusinessType.DELETE)
	@PostMapping("/broadcastMessage")
    public AjaxResult broadcastMessage(@RequestBody String text)
    {
        return toAjax(tMessagesService.broadcastMessage(text));
    }

    /**
     * 广播消息
     */
    @Log(title = "广播消息历史消息", businessType = BusinessType.DELETE)
	@PostMapping("/broadcastMessageList")
    public TableDataInfo broadcastMessageList()
    {
        startPage();
        List<SendHistory> list = tMessagesService.broadcastMessageList();
        return getDataTable(list);

    }

    /**
     * 群发消息
     */
    @Log(title = "群发消息", businessType = BusinessType.DELETE)
    @PostMapping("/sendGroupMessage")
    public AjaxResult sendGroupMessage(@RequestBody GroupSendMessage message)
    {
        return toAjax(tMessagesService.groupMessage(message));
    }
    /**
     * 群发消息记录列表
     */
    @Log(title = "群发消息记录列表", businessType = BusinessType.DELETE)
    @GetMapping("/sendGroupMessage/list")
    public TableDataInfo sendGroupMessageList()
    {
        startPage();
        List<SendHistory> list = tMessagesService.sendGroupMessageList();
        return getDataTable(list);
    }
}
